const app = getApp()
var WXBizDataCrypt = require('../../../utils/RdWXBizDataCrypt.js');
var AppId = 'wxwxaa93ef54763f94c9'
var AppSecret = '4ccb16180f4157292fc2790f11cfd433'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName:"",
    passWord:"",
    phone: "",
    quickPh: '',
    showModal: false,
    quickpw: ''
  },
  showXUser: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },
  showXPass: function (e) {
    this.setData({
      passWord: e.detail.value
    })
  },
  hideXUser: function (e) {
    this.setData({
      userName: ""
    })
  },
  hideXPass: function (e) {
    this.setData({
      passWord: ""
    })
  },
  changeText: function (e) {
    let name = e.currentTarget.dataset.name;
    let nameMap = {}
    nameMap[name] = e.detail && e.detail.value
    this.setData(nameMap)
  },
  cancelBtn: function () {
    this.setData({
      showModal: false,
      quickpw: ''
    })
  },
  checkBtn: function () {
    var that = this
    app.req({
      url: "User/CustomerWchatRegister",
      method: "POST",
      data: {
        LoginNo: this.data.quickPh,
        Password: this.data.quickpw
      },
      success: function (res) {
        if (res.data.Code) {
          that.setData({
            showModal: false,
            quickpw: ''
          })
          wx.showModal({
            content: '注册成功，请在管理员审批权限后登录',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              }
            }
          });
        } else {
          wx.showModal({
            content: '该手机已注册，请在管理员审批权限后登录',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              }
            }
          });
        }
      }
    })
  },
  login: function(e){
    // this.setData({
    //   userName: "guanzc",
    //   passWord: "huqian456"
    // })
    var that = this
    console.log(e)
    if (this.data.userName == ''){
      wx.showToast({
        title: '请输入账号',
        success: function(){
          setTimeout(function(){
            wx.hideToast()
          },1000)
        }
      })
    } else if (this.data.passWord == '') {
      wx.showToast({
        title: '请输入密码',
        success: function () {
          setTimeout(function () {
            wx.hideToast()
          }, 1000)
        }
      })
    } else if (this.data.userName != '' && this.data.passWord != ''){
      wx.request({
        url: app.globalData.api + 'Customer/Login',
        method: "POST",
        data: {
          "loginNo": this.data.userName,
          "password": this.data.passWord,
          "loginSource": "customer"
        },
        success: function (res) {
          console.log(res)
          if (res.data && res.data.jwtBearer) {
            app.globalData.Authorization = res.data.jwtBearer
            // wx.setStorage({
            //   key: 'Authorization',
            //   data: res.data.jwtBearer,
            // })
            wx.setStorageSync('LoginNo', that.data.userName)
            wx.setStorageSync('Password', that.data.passWord)
            wx.switchTab({
              url: '/pages/operate/operate'
            })
          } else {
            wx.showToast({
              title: '登录错误',
              icon: 'none',
              duration: 2000,
              mask: true
            })
          }
        }
      })
    }
    
    
  },
  getPhoneNumber: function (e) {
    var that = this
    wx.login({
      success: function (data) {
        //发起网络请求
        console.log(data)
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + app.globalData.appId + '&secret=' + app.globalData.sceret + '&js_code=' + data.code + '&grant_type=authorization_code',
          data: {},
          header: {
            "Content-Type": "json"
          },
          method: 'GET',
          success: function (res) {
            var pc = new WXBizDataCrypt(AppId, res.data.session_key)
            var data = pc.decryptData(e.detail.encryptedData, e.detail.iv)
            console.log('解密后 data: ', data)
            app.req({
              url: "User/CustomerWchat/IsExistence/" + data.phoneNumber,
              method: "POST",
              success: function(res){
                if (res.data.Code) {
                  that.setData({
                    quickPh: data.phoneNumber,
                    showModal: true
                  })
                } else {
                  wx.showModal({
                    content: '该手机已注册，请在管理员审批权限后登录',
                    showCancel: false,
                    success: function (res) {
                      if (res.confirm) {
                        console.log('用户点击确定')
                      }
                    }
                  });
                }
              }
            })
          },
          fail: function (res) { },
          complete: function (res) { }
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.Authorization)
    if (app.globalData.Authorization && app.globalData.Authorization!=''){
      wx.switchTab({
        url: '/pages/operate/operate'
      })
    }
    // var that = this
    // app.globalData.LoginNo = wx.getStorageSync('LoginNo')
    // app.globalData.Password = wx.getStorageSync('Password')
    // console.log(app.globalData.LoginNo, app.globalData.Password)
    // if (app.globalData.LoginNo != '' && app.globalData.Password != '') {
    //   wx.request({
    //     url: app.globalData.api + 'User/Login',
    //     method: "POST",
    //     data: {
    //       "loginNo": app.globalData.LoginNo,
    //       "password": app.globalData.Password,
    //       "loginSource": "customer"
    //     },
    //     success: function (res) {
    //       if (res.data.Code) {
    //         app.globalData.Authorization = res.data.Result
    //         wx.setStorage({
    //           key: 'Authorization',
    //           data: res.data.Result,
    //         })
    //         wx.switchTab({
    //           url: '/pages/operate/operate'
    //         })
    //       }
    //     }
    //   })
    // }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})